package com.neusoft.nep.grid.controller;




import com.neusoft.nep.common.FileUtils;
import com.neusoft.nep.grid.caluate.AlertUtils;
import com.neusoft.nep.grid.entity.LoginInfo;
import com.neusoft.nep.grid.entity.User;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.paint.Color;
import com.neusoft.nep.grid.entity.Fankui;

import java.util.HashMap;
import java.util.Map;

import static com.neusoft.nep.grid.caluate.CaluateAQI.caluateAQI;
import static com.neusoft.nep.grid.caluate.CaluateAQI.givelevel;
import static com.neusoft.nep.grid.caluate.CaluateCOAQI.caluateCOAQI;
import static com.neusoft.nep.grid.caluate.CaluatePM25AQI.caluatePM25AQI;
import static com.neusoft.nep.grid.caluate.CaluateSO2AQI.caluateSO2AQI;
import static com.neusoft.nep.common.FileUtils.readMapObject;
import static com.neusoft.nep.grid.entity.Fankui.*;

public class AQIfankuiController {
    @FXML
    private TextField SO2 ,CO ,PM25,Code;
    @FXML
    private Label SO2AQI,COAQI,PM25AQI,AQI,logincode;
    @FXML
    private TableView tableView;
    @FXML
    private TableColumn<User,String> colcode,colreturnperson,coltime,collevel,colsheng,colshi;

    private ObservableList tableList= FXCollections.observableArrayList();
    private Map userMap=new HashMap();
    private String loginCode;

    @FXML
    public void initialize(){
        loginCode=LoginInfo.loginCode;
        tableView.setItems(tableList);
        colcode.setCellValueFactory(new PropertyValueFactory<>("code"));
        colreturnperson.setCellValueFactory(new PropertyValueFactory<>("returnperson"));
        coltime.setCellValueFactory(new PropertyValueFactory<>("time"));
        collevel.setCellValueFactory(new PropertyValueFactory<>("level"));
        colsheng.setCellValueFactory(new PropertyValueFactory<>("sheng"));
        colshi.setCellValueFactory(new PropertyValueFactory<>("shi"));
        String filename="Fankui"+loginCode+".txt";
        userMap= readMapObject(filename);
        tableList.addAll(userMap.values());
        logincode.setText(loginCode);
    }
    public void caluate(){
        String filename="Fankui"+loginCode+".txt";
        //计算模块
        String SO2density=SO2.getText();
        String COdensity=CO.getText();
        String PM25density=PM25.getText();
        String Aqi=null;
        String code=Code.getText();
        int SO2=Integer.parseInt(SO2density);
        int CO=Integer.parseInt(COdensity);
        int PM25=Integer.parseInt(PM25density);
        if (caluateSO2AQI(SO2) == 1) {
            SO2AQI.setTextFill(Color.GREEN);
        } else if (caluateSO2AQI(SO2) == 2) {
            SO2AQI.setTextFill(Color.KHAKI);
        } else if (caluateSO2AQI(SO2) == 3) {
            SO2AQI.setTextFill(Color.ORANGE);
        } else if (caluateSO2AQI(SO2) == 4) {
            SO2AQI.setTextFill(Color.RED);
        } else if (caluateSO2AQI(SO2) == 5) {
            SO2AQI.setTextFill(Color.PURPLE);
        } else if (caluateSO2AQI(SO2) == 6) {
            SO2AQI.setTextFill(Color.BROWN);
        }
        if(caluateCOAQI(CO)==1){
            COAQI.setTextFill(Color.GREEN);
        }else if(caluateCOAQI(CO)==2){
            COAQI.setTextFill(Color.KHAKI);
        }else if(caluateCOAQI(CO)==3){
            COAQI.setTextFill(Color.ORANGE);
        }else if(caluateCOAQI(CO)==4){
            COAQI.setTextFill(Color.RED);
        }else if(caluateCOAQI(CO)==5){
            COAQI.setTextFill(Color.PURPLE);
        }else if(caluateCOAQI(CO)==6){
            COAQI.setTextFill(Color.BROWN);
        }
        if(caluatePM25AQI(PM25)==1){
            PM25AQI.setTextFill(Color.GREEN);
        }else if(caluatePM25AQI(PM25)==2){
            PM25AQI.setTextFill(Color.KHAKI);
        }else if(caluatePM25AQI(PM25)==3){
            PM25AQI.setTextFill(Color.ORANGE);
        }else if(caluatePM25AQI(PM25)==4){
            PM25AQI.setTextFill(Color.RED);
        }else if(caluatePM25AQI(PM25)==5){
            PM25AQI.setTextFill(Color.PURPLE);
        }else if(caluatePM25AQI(PM25)==6){
            PM25AQI.setTextFill(Color.BROWN);
        }
        if(caluateAQI(caluateSO2AQI(SO2),caluateCOAQI(CO),caluatePM25AQI(PM25))==1){
            AQI.setTextFill(Color.GREEN);
        }else if(caluateAQI(caluateSO2AQI(SO2),caluateCOAQI(CO),caluatePM25AQI(PM25))==2){
            AQI.setTextFill(Color.KHAKI);
        }else if(caluateAQI(caluateSO2AQI(SO2),caluateCOAQI(CO),caluatePM25AQI(PM25))==3){
            AQI.setTextFill(Color.ORANGE);
        }else if(caluateAQI(caluateSO2AQI(SO2),caluateCOAQI(CO),caluatePM25AQI(PM25))==4){
            AQI.setTextFill(Color.RED);
        }else if(caluateAQI(caluateSO2AQI(SO2),caluateCOAQI(CO),caluatePM25AQI(PM25))==5){
            AQI.setTextFill(Color.PURPLE);
        }else if(caluateAQI(caluateSO2AQI(SO2),caluateCOAQI(CO),caluatePM25AQI(PM25))==6){
            AQI.setTextFill(Color.BROWN);
        }
        SO2AQI.setText(givelevel(caluateSO2AQI(SO2)));
        COAQI.setText(givelevel(caluateCOAQI(CO)));
        PM25AQI.setText(givelevel(caluatePM25AQI(PM25)));
        AQI.setText(String.valueOf(givelevel(caluateAQI(caluateSO2AQI(SO2),caluateCOAQI(CO),caluatePM25AQI(PM25)))));

        Aqi=givelevel(caluateAQI(caluateSO2AQI(SO2),caluateCOAQI(CO),caluatePM25AQI(PM25)));
//查找文件中的特定对象
        String aqiValue = Aqi;
        Fankui fankui = findfankuiByCode(filename, code);
        Fankui newFankui = new Fankui(
                fankui.getCode(),
                fankui.getReturnperson(),
                fankui.getTime(),
                fankui.getLevel(),
                fankui.getSheng(),
                fankui.getShi(),
                aqiValue
        );

        // 保存到新文件
        saveToNewFile(code, newFankui);
        //删除表格及文件中的已完成AQI上传的对象
        tableList.removeIf(item ->
                item instanceof Fankui && ((Fankui) item).getCode().equals(code)
        );
        removeProcessedObject(filename, code);

    }
    private void removeProcessedObject(String filename, String code) {
        try {
            Map<String, Fankui> allObjects = FileUtils.readMapObject(filename);
            if (allObjects.remove(code) != null) {
                FileUtils.writeMapObject(filename, allObjects);
                System.out.println("已从原文件中删除Code为 " + code + " 的对象");
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("删除原文件中的对象时出错: " + e.getMessage());
        }

    }}


